package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.GoodsDto;
import com.woniuxy.goods.dto.GroupTypeDto;
import com.woniuxy.goods.entity.ProcessExecute;
import com.woniuxy.goods.form.AddExecuteForm;
import com.woniuxy.goods.form.FindGoodsPageForm;
import com.woniuxy.goods.param.AddExecuteParam;
import com.woniuxy.goods.param.FindGoodsPageParam;
import com.woniuxy.goods.service.ProcessExecuteService;
import com.woniuxy.goods.util.TokenUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 * 流程执行记录控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "典当流程执行记录管理接口")
@RequestMapping("/processExecute")
public class ProcessExecuteController {
    /**
     * 流程执行记录服务层接口
     */
    @Resource
    private ProcessExecuteService processExecuteService;
    /**
     * 解析token工具类
     */
    @Resource
    private TokenUtil tokenUtil;

    /**
     * 新增一系列流程
     * @param form 新增流程执行记录对象
     * @return
     * @throws Exception
     */
    @ApiOperation("新增一系列流程记录接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = ProcessExecute.class)
    })
    @PostMapping("/addExecute")
    public Result addExecute(@Validated @RequestBody AddExecuteForm form, BindingResult br) throws Exception{
        if(br.hasErrors())throw  new ParamException(10001,"参数有误");
        List<ProcessExecute> executes = processExecuteService.addExecute(BeanUtil.toBean(form, AddExecuteParam.class));
        return Result.success("新增一系列流程成功",executes);
    }

    /**
     * 根据流程执行id修改单个流程活跃状态
     * @param processExecuteId 流程执行id
     * @return 流程执行对象
     * @throws Exception
     */
    @ApiOperation("根据流程执行id修改单个流程活跃状态")
    @ApiImplicitParam("流程执行id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = ProcessExecute.class)
    })
    @GetMapping("/updateActiveById")
    public Result updateActiveById(Integer processExecuteId) throws Exception{
        ProcessExecute executes = processExecuteService.updateActiveById(processExecuteId);
        return Result.success("根据流程执行id修改单个流程活跃状态成功",executes);
    }

    /**
     * 流程向下一级活跃
     * @param processExecuteId 流程执行id
     * @return 流程执行对象
     * @throws Exception
     */
    @ApiOperation("流程向下一级活跃")
    @ApiImplicitParams({
            @ApiImplicitParam("流程执行id")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = ProcessExecute.class)
    })
    @GetMapping("/updateUpActiveById")
    public Result updateUpActiveById(Integer processExecuteId) throws Exception{
        ProcessExecute executes = processExecuteService.updateUpActiveById(processExecuteId);
        return Result.success("流程向下一级活跃成功",executes);
    }

    /**
     * 流程回退一级活跃
     * @param processExecuteId 流程执行id
     * @return 流程执行对象
     * @throws Exception
     */
    @ApiOperation("流程回退一级活跃")
    @ApiImplicitParam("流程执行id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = ProcessExecute.class)
    })
    @GetMapping("/updateDownActiveById")
    public Result updateDownActiveById(Integer processExecuteId) throws Exception{
        ProcessExecute executes = processExecuteService.updateDownActiveById(processExecuteId);
        return Result.success("流程回退一级活跃成功",executes);
    }

    /**
     * 修改指定物品流程
     * @param goodsId 物品id
     * @param processExecuteId 流程执行id
     * @return 流程执行对象
     * @throws Exception
     */
    @ApiOperation("修改指定物品流程")
    @ApiImplicitParams({
            @ApiImplicitParam("物品id"),
            @ApiImplicitParam("流程执行id")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = ProcessExecute.class)
    })
    @GetMapping("/updateProcessByGoodsId")
    public Result updateDownActiveById(Integer goodsId,Integer processExecuteId) throws Exception{
        ProcessExecute executes = processExecuteService.updateProcessByGoodsId(goodsId,processExecuteId);
        return Result.success("流程回退一级活跃成功",executes);
    }

    /**
     * 修改账户id对应待办流程
     * @param accountId 账户id
     * @return 流程集合
     * @throws Exception
     */
    @ApiOperation("修改账户id对应待办流程")
    @ApiImplicitParam("账号id")
    @GetMapping("/updateProcessByAccountId")
    public Result updateProcessByAccountId(Integer accountId) throws Exception{
        List<ProcessExecute> executes = processExecuteService.updateProcessByAccountId(accountId);
        return Result.success("修改账户id对应待办流程成功",executes);
    }

    /**
     * 修改店长对应待办流程
     * @param oldAccountId 旧店长id
     * @param newAccountId 新店长id
     * @return 流程集合
     * @throws Exception
     */
    @ApiOperation("修改店长对应待办流程")
    @ApiImplicitParams({
            @ApiImplicitParam("旧店长id"),
            @ApiImplicitParam("新店长id")
    })
    @GetMapping("/updateProcessByBoss")
    public Result updateProcessByBoss(Integer oldAccountId,Integer newAccountId) throws Exception{
        List<ProcessExecute> executes = processExecuteService.updateProcessByBoss(oldAccountId,newAccountId);
        return Result.success("修改店长对应待办流程成功",executes);
    }

    /**
     * 根据流程码、商品id查流程
     * @param stateNum 流程码
     * @param goodsId 物品id
     * @return 流程执行对象
     * @throws Exception
     */
    @ApiOperation("根据流程码、商品id查流程")
    @ApiImplicitParams({
            @ApiImplicitParam("流程码"),
            @ApiImplicitParam("物品id")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = ProcessExecute.class)
    })
    @GetMapping("/findExecuteByStateNumGoodsId")
    public Result findExecuteByStateNumGoodsId(Integer stateNum,Integer goodsId) throws Exception{
        ProcessExecute execute = processExecuteService.findExecuteByStateNumGoodsId(stateNum,goodsId);
        return Result.success("根据流程码、商品id查流程成功",execute);
    }

    /**
     * 根据流程码、管理员id查物品id集合
     * @param stateNum 流程码
     * @param token 携带当前登录管理员id的请求头
     * @return 物品id集合
     * @throws Exception
     */
    @ApiOperation("根据流程码、管理员id查物品id集合")
    @ApiImplicitParams({
            @ApiImplicitParam("流程码"),
            @ApiImplicitParam("携带当前登录管理员id的请求头")
    })
    @GetMapping("/findGoodsIdsByStateNum")
    public Result findGoodsIdsByStateNum(Integer stateNum,@RequestHeader String token) throws Exception{
        Integer accountId = tokenUtil.getAccountId(token);
        List<Integer> goodsIds = processExecuteService.findGoodsIdsByStateNum(stateNum,accountId);
        return Result.success("根据流程码、管理员id查物品id集合成功",goodsIds);
    }

    /**
     * 根据物品id查物品流程集合
     * @param goodsId 物品id
     * @return 物品流程集合
     * @throws Exception
     */
    @ApiOperation("根据物品id查物品流程集合")
    @ApiImplicitParam("物品id")
    @GetMapping("/findProcessByGoodsId")
    public Result findProcessByGoodsId(Integer goodsId) throws Exception{
        List<ProcessExecute> executes = processExecuteService.findProcessByGoodsId(goodsId);
        return Result.success("根据物品id查物品流程集合成功",executes);
    }

    /**
     * 根据账号id查询待办流程
     * @param token 携带账号id的请求头
     * @param form 查询物品分页参数
     * @return 物品分页对象
     * @throws Exception
     */
    @ApiOperation("根据账号id查询待办流程")
    @ApiImplicitParam("账号id")
    @PostMapping("/findProcessByAccountId")
    public Result findProcessByAccountId(@RequestBody FindGoodsPageForm form,@RequestHeader String token) throws Exception{
        Integer accountId = tokenUtil.getAccountId(token);
        FindGoodsPageParam param = BeanUtil.toBean(form, FindGoodsPageParam.class);
        IPage<GoodsDto> ipage = processExecuteService.findProcessByAccountId(accountId,param);
        return Result.success("根据账号id查询待办流程成功",ipage);
    }

}

